package com.tanhua.management.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.common.pojo.Admin;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class AdminService {


    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Value("${jwt.secret}")
    private String secret;
    /**
     * 通过sso的rest接口查询
     *
     * @param token
     * @return
     */
    public Admin queryAdminByToken(String token) {

            try {
                // 通过token解析数据
                Map<String, Object> body = Jwts.parser()
                        .setSigningKey(secret)
                        .parseClaimsJws(token)
                        .getBody();

                Admin admin = new Admin();
                admin.setId(Long.valueOf(body.get("id").toString()));
                return admin;
            } catch (ExpiredJwtException e) {
                log.info("token已经过期！ token = " + token);
            } catch (Exception e) {
                log.error("token不合法！ token = "+ token, e);
            }
            return null;
    }

}
